/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var Prototype = {
	Browser: {
		IE: !!(window.attachEvent && !window.opera),
		Opera: !!window.opera,
		WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
		Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
		MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
	}
};
/*
*------Selection--------------------------------------------------
*/
function $(id) {
	return (typeof(id) == "string") ? document.getElementById(id) : id;
}
/*
*------Object--------------------------------------------------
*/
Object.extend = function(destination, source) {
	for (var property in source)
	destination[property] = source[property];
	return destination;
};
/*
*------Fonction--------------------------------------------------
*/
Object.extend(Function.prototype, {
	bind : function() {
		var __method = this, args = Array.prototype.slice.call(arguments,0), object = args.shift();
		return function() {
			return __method.apply(object, args.concat(Array.prototype.slice.call(arguments,0)));
		}
	},
	bindAsEventListener : function() {
		var __method = this, args = Array.prototype.slice.call(arguments,0), object = args.shift();
		return function(event) {
			return __method.apply(object, [( event || window.event)].concat(args).concat(Array.prototype.slice.call(arguments,0)));
		}
	}
});
/*
*------Evenement--------------------------------------------------
*/
var Event = {
	observe : function() {
		if(window.addEventListener){
			return function(element, eventName, handler) {
				$(element).addEventListener(eventName, handler, false);
			};
		}else if(window.attachEvent){
			return function(element, eventName, handler) {
				$(element).attachEvent("on"+eventName, handler);
			};
		}else{
			return function(element, eventName, handler) {};
		}
	}(),
	stopObserving : function() {
		if(window.removeEventListener){
			return function(element, eventName, handler) {
				$(element).removeEventListener(eventName, handler, false);
			};
		}else if(window.detachEvent){
			return function(element, eventName, handler) {
				$(element).detachEvent("on"+eventName, handler);
			};
		}else{
			return function(element, eventName, handler) {};
		}
	}(),
	pointer: function(event) {
		return {
			x: event.pageX || (event.clientX +
			(document.documentElement.scrollLeft || document.body.scrollLeft)),
			y: event.pageY || (event.clientY +
			(document.documentElement.scrollTop || document.body.scrollTop))
		};
	}
};
/*
*------Element--------------------------------------------------
*/
var Element = new Object();
Object.extend(Element, {
	viewportOffset: function(forElement) {
		var forElement=$(forElement);
		var element = forElement;
		var value = [0,0];
		do {
		value[0]+=element.offsetLeft || 0;
		value[1]+=element.offsetTop || 0;
		element = element.offsetParent;
		} while (element);
		element = forElement;
		do {
		if( element.tagName != 'HTML'){
		value[1]-=element.scrollTop || 0;
		value[0]-=element.scrollLeft || 0;
		}
		} while (element = element.parentNode);
		return value;
	}
});

/* Fonction immitant la balise HTML <marquee> non valide W3C */
function Marquee(box, content, speed, dirc, coefOverSpeed){
	/*
	liste des arguments possibles :
	- id ou element conteneur
	- id ou element contenue
	- vitesse initiale
	- direction du déplacement
	- vitesse du scrolling , lors d'un mouse mouve au dessus de l 'élement
	*/

	//ini les variables
	this.box=$(box);
	this.content=$(content);
	this.speed=speed || 1;
	this.dirc=dirc || 'top';
	this.coefDirc=1;
	this.coefOverSpeed=coefOverSpeed || 3;
	this.inverseDirc=(this.dirc=='bottom' || this.dirc=='right');
	var horizontalDirc=(this.dirc=='bottom' || this.dirc=='top');

	//on repositionne les élements + cache les scrolls
	this.box.style.overflow='hidden';
	this.box.style.position='relative';//ie7 bug
	this.content.style.position='relative';

	//calcule la dimension du conteneur + la dimention du contenue
	var boxDim=this.box['client'+(horizontalDirc?'Height':'Width')];
	var contentDim=this.content['offset'+(horizontalDirc?'Height':'Width')];

	//on retient la moitier de la dim , pour le mouse over
	this.middleDim=boxDim/2;

	//on definit las position max et de départ
	this.maxDim=this.inverseDirc?-boxDim:-contentDim;
	this.startStep=this.inverseDirc?contentDim:boxDim;
	this.currentStep=this.startStep;

	//ajoutes les evenemnts
	this.eventOver=this.onMouseover.bindAsEventListener(this);
	Event.observe(this.box,'mouseover',this.eventOver);
	this.eventOut=this.onMouseout.bindAsEventListener(this);
	Event.observe(this.box,'mouseout',this.eventOut);
	this.interval=setInterval(this.interval.bind(this),35);
	this.eventUnload=this.unload.bindAsEventListener(this);
	Event.observe(window,'unload',this.eventUnload.bind(this));
}

Marquee.prototype={
	onMouseout : function(e){
		Event.stopObserving(this.box,'mousemove',this.eventMouseMove);
		this.coefDirc=1;
	},
	onMouseover : function(e){
		var dim=Element.viewportOffset(this.box);
		if(this.dirc=='top' || this.dirc=='bottom'){
		this.coor=[dim[1],dim[1]+this.box.clientHeight];
		}else this.coor=[dim[0],dim[0]+this.box.clientWidth];
		this.mouseMove(e);
		this.eventMouseMove=this.mouseMove.bindAsEventListener(this);
		Event.observe(this.box,'mousemove',this.eventMouseMove);
	},
	interval : function(){
		var acc=this.speed*this.coefDirc;
		$(this.content).style[this.dirc]=this.currentStep-acc+'px';
		this.currentStep-=acc;
		if(this.currentStep>this.startStep)
		this.currentStep=this.maxDim;
		if(this.inverseDirc){
		if(this.currentStep<this.maxDim)
		this.currentStep=this.startStep;
		}else{
		if(this.currentStep<this.maxDim)
		this.currentStep=this.startStep;
		}
	},
	mouseMove:function(e){
		if(this.dirc=='top' || this.dirc=='bottom'){
		var mouseCoor=Event.pointer(e).y;
		}else var mouseCoor=Event.pointer(e).x;
		var inverseCoeff=(mouseCoor-this.coor[0]>this.coor[1]-mouseCoor);
		var coef=inverseCoeff?this.coor[1]-mouseCoor:mouseCoor-this.coor[0];
		this.coefDirc=((this.middleDim-coef)/this.middleDim)*this.coefOverSpeed;
		if(inverseCoeff)this.coefDirc=-this.coefDirc;
		if(this.inverseDirc)this.coefDirc=-this.coefDirc;
	},
	unload : function(){
		clearInterval(this.interval);
		Event.stopObserving(this.box,'mouseover',this.eventOver);
		Event.stopObserving(this.box,'mouseout',this.eventOut);
		Event.stopObserving(window,'unload',this.eventUnload.bind(this));
	}
}

/* Fonction qui change d'image à la manière d'une GIF */
couv = [];
couv[0] = 'resources/images/campus.jpg';
couv[1] = 'resources/images/campus2.jpg';
couv[2] = 'resources/images/campus3.jpg';
couv[3] = 'resources/images/campus4.jpg';
num = 2;
function ChangeImage(){
	if (num == -1){ num = (couv.length-1)}
	if(document.getElementById){
		document.getElementById("welcome").innerHTML = '<img src="'+couv[num]+'" alt="Photos campus"><img src="resources/images/etudiants.jpg" alt="Photos etudiants">';
		num = num-1;
	}
	setTimeout("ChangeImage()", 3000);
}
